package dic.manage;

import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import net.sf.json.JSONObject;

import org.json.JSONArray;

import dic.model.City;
import dic.model.Province;
import dic.persistence.DicDao;

/*
 * 
 * 字典管理实现类
 */
public class DicManageImpl implements DicManage {

    private DicDao dicDao=null;
	public DicDao getDicDao() {
		return dicDao;
	}
	public void setDicDao(DicDao dicDao) {
		this.dicDao = dicDao;
	}
	/**
	 * 获得省
	 * @return 返回字符json字符串
	 */
	public JSONObject getCity(int pid) {
		List list=dicDao.getCityList(pid);
		StringBuffer sb=new StringBuffer();
		for(Iterator it=list.iterator();it.hasNext();)
		{
			City c=(City)it.next();
			sb.append("<option value='"+c.getId()+"'>"+c.getName()+"</option>");	
		}
		JSONObject jsonObject=new JSONObject();
        jsonObject.put("city",sb.toString());
          return jsonObject;
	}
	/**
	 * 获得城市
	 * @return 返回字符json字符串
	 */
	public JSONObject getProvince() {
		List list=dicDao.getProvince();
		StringBuffer sb=new StringBuffer();
		for(Iterator it=list.iterator();it.hasNext();)
		{
			Province p=(Province)it.next();
			sb.append("<option value='"+p.getId()+"'>"+p.getName()+"</option>");	
		}
		JSONObject jsonObject=new JSONObject();
		           jsonObject.put("province",sb.toString());
		return jsonObject;
	}
	/**
	 * 获得city
	 */
	public City findCityByName(String name) {
		
		return dicDao.findCityByName(name);
	}
	/**
	 * 获得city by id
	 */
	public City findCityById(int id) {
		
		return dicDao.findCityById(id);
	}
    /**
     * 获得城市列表
     */
	
	public Map<String,List<City>> getProvinceCityList() {
		 Map<String,List<City>>  mapProvinceCity=new HashMap<String,List<City>>();
		
		List listProvince=dicDao.getProvince();
		for(Iterator it=listProvince.iterator();it.hasNext();)
		{
			Province provine=(Province)it.next();
			List list=dicDao.getCityList(provine.getId());
			mapProvinceCity.put(provine.getName(),list);
		 }
		
		return mapProvinceCity;
	}
	/**
	 * 选择开通城市
	 */
	public JSONObject dredge(int id) {
		dicDao.dredge(id);
		 JSONObject jsonObject=new JSONObject();
		 jsonObject.put("success", true);
		
		return jsonObject;
	}
	/**
	 * 获得开放的城市
	 */
	public List getDredgeCitys() {
		List list=dicDao.getDredgeCitys();
		return list;
	}
	/**
	 * 获得Category
	 */
	public JSONObject getCategoryList(int flag) {
		List list=dicDao.getCategoryList(flag);
		JSONObject json=new JSONObject();
		json.put("category",list);
		return json;
	}
	/**
	 * 获得Category
	 */
	@SuppressWarnings("unchecked")
	public List getCategoryForList(int flag) {
		List list=dicDao.getCategoryList(flag);
		
		return list;
	}
	/**
	 * 获得category by id List
	 */
	public List getProvinceCityByIdForList(String categoryId) {
           List list=dicDao.getProvinceCityByIdForList(categoryId);
		
		return list;
	}
	/**
	 * 获得品牌用户类型列表
	 */
	public JSONObject getCategoryList(String categoryId) {
        List list=dicDao.getCategoryList(categoryId);
		JSONObject json=new JSONObject();
		   json.put("success",list);
		return json;
	}
	/**
	 * 获得city option
	 */
	public JSONObject getDredgeCitysOption() {
		List list=dicDao.getDredgeCitys();
		String option="<option value=''>全部</option>";
		for(int i=0;i<list.size();i++)
		{
			City city=(City)list.get(i);
			option+="<option value='"+city.getId()+"'>"+city.getName()+"</option>";
		}
		JSONObject json=new JSONObject();
		   json.put("success",option);
		return json;
		
	}
}
